package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.Device;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
public class RootController {
    
    @Autowired
    private DeviceService deviceService;
    
    /**
     * 根路径访问所有设备信息的接口（解决404问题）
     * URL: http://127.0.0.1:8081/findAll
     * Method: GET
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Device> devices = deviceService.findAllDevices();
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据设备名称查询设备信息的全局接口
     * URL: http://127.0.0.1:8081/search?deviceName=空调
     * Method: GET
     */
    @GetMapping("/search")
    public Result searchDevicesByName(@RequestParam String deviceName) {
        try {
            List<Device> devices = deviceService.findDevicesByName(deviceName);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 从MongoDB中根据设备名称查询设备的全局接口
     * URL: http://127.0.0.1:8081/searchMongo?deviceName=空调
     * Method: GET
     */
    @GetMapping("/searchMongo")
    public Result searchDevicesByNameInMongo(@RequestParam String deviceName) {
        try {
            List<Device> devices = deviceService.findDevicesByNameContainingInMongo(deviceName);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("从MongoDB查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据状态查询MongoDB中的设备的全局接口
     * URL: http://127.0.0.1:8081/searchByStatus?status=0
     * Method: GET
     */
    @GetMapping("/searchByStatus")
    public Result searchDevicesByStatus(@RequestParam Integer status) {
        try {
            List<Device> devices = deviceService.findDevicesByStatus(status);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("从MongoDB查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据状态和时间范围查询MongoDB中的设备的全局接口
     * URL: http://127.0.0.1:8081/searchByStatusAndTimeRange?status=0&startTime=2023-01-01T00:00:00&endTime=2023-12-31T23:59:59
     * Method: GET
     */
    @GetMapping("/searchByStatusAndTimeRange")
    public Result searchDevicesByStatusAndTimeRange(
            @RequestParam Integer status,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date endTime) {
        try {
            List<Device> devices = deviceService.findDevicesByStatusAndTimeRange(status, startTime, endTime);
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("从MongoDB查询设备信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 调试接口：查看MongoDB中的所有设备信息
     * URL: http://127.0.0.1:8081/debug
     * Method: GET
     */
    @GetMapping("/debug")
    public Result debug() {
        try {
            List<Device> devices = deviceService.findAll();
            return Result.success(devices);
        } catch (Exception e) {
            return Result.error("调试失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据设备ID删除MongoDB中的设备
     * URL: http://127.0.0.1:8081/deleteDevice?deviceId=1
     * Method: DELETE
     */
    @DeleteMapping("/deleteDevice")
    public Result deleteDevice(@RequestParam Integer deviceId) {
        try {
            deviceService.deleteDeviceById(deviceId);
            return Result.success("设备删除成功");
        } catch (Exception e) {
            return Result.error("删除设备失败: " + e.getMessage());
        }
    }
}